﻿#ifndef XRTCCLIENT_RENDER_D3D9_RENDER_H_
#define XRTCCLIENT_RENDER_D3D9_RENDER_H_

#include <rtc_base/win32.h>
#include <api/media_stream_interface.h>

struct IDirect3D9;
struct IDirect3DDevice9;
struct IDirect3DSurface9;

namespace xrtc {

class D3D9Render : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
public:
    D3D9Render(void* hwnd,rtc::VideoSourceInterface<webrtc::VideoFrame>* track_to_render);
    ~D3D9Render() override;

    void Start();
    void Stop();
    void Resize();

    // rtc::VideoSinkInterface
    void OnFrame(const webrtc::VideoFrame& frame) override;

private:
    bool TryInit(const webrtc::VideoFrame& frame);
    void DoRender(const webrtc::VideoFrame& frame);

private:
    // 渲染的窗口句柄
    void* hwnd_;
    // 视频源
    rtc::VideoSourceInterface<webrtc::VideoFrame>* track_to_render_;
    bool running_ = false;

    IDirect3D9* d3d9_ = nullptr;
    IDirect3DDevice9* d3d9_device_ = nullptr;
    IDirect3DSurface9* d3d9_surface_ = nullptr;

    int width_ = 640;
    int height_ = 360;

    char* rgb_buffer_ = nullptr;
    size_t rgb_buffer_size_ = 0;

    RECT render_rect_;
};

} // namespace xrtc

#endif // XRTCCLIENT_RENDER_D3D9_RENDER_H_